﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using Power.View;
using Power.Models;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace Power.ViewModel
{
    public class ProjectOverViewModel:ViewModelBase
    {
        public ProjectOverview model;
        public ProOverviewEntity Data { get; private set; }
        public ProjectOverViewModel(ProjectOverview model)
        {
            this.model = model;
            this.model.SizeChanged += Model_SizeChanged;
            OpenBetch = new RelayCommand(OnOpenBetch);
            var innerRadius = 30;
            this.model.lvc1.InnerRadius = innerRadius;
            this.model.lvc2.InnerRadius = innerRadius;
            this.model.lvc3.InnerRadius = innerRadius;
            this.model.lvc4.InnerRadius = innerRadius;
            Data = new ProOverviewEntity()
            {
                ProName = InitProName("项目测试长度是9啦"),
                Area = "01",
                Block = "02",
                WorkField = "11",
                DefectBat = "05",
                DetectLen = 32.2,
                DefectPipNum = 12,
                StruDefectNum = 475,
                FuncDefectNum = 242,
                BatchList = new List<string> { "05","06","07"},
                DefectDense = new List<DefectDenseChartData>()
                {
                    new DefectDenseChartData(){Label="1级",Value=1.4},
                    new DefectDenseChartData(){Label="2级",Value=2.4},
                    new DefectDenseChartData(){Label="3级",Value=3.4},
                    new DefectDenseChartData(){Label="4级",Value=4.4}
                },

                DefectLv1 = 10,
                DefectLv2 = 20,
                DefectLv3 = 30,
                DefectLv4 = 40,
                DefectKind = new List<DefectKindTableData>()
                {
                    new DefectKindTableData(){ 
                        DefectType = "结构性",
                        Value = new List<DefectKindBaseValue>()
                        {
                            new DefectKindBaseValue(){ 
                                DefectName = "破裂",
                                Lv1 = 1,
                                Lv2 = 2,
                                Lv3 = 3,
                                Lv4 = 4,
                                Sum = 10,
                                Remark = ""
                            },
                            //new DefectKindBaseValue(){
                            //    DefectName = "渗漏",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "异物穿入",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "支管暗接",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "起伏",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "变形",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "脱节",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "脱落",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "错口",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //},
                            //new DefectKindBaseValue(){
                            //    DefectName = "腐蚀",
                            //    Lv1 = 1,
                            //    Lv2 = 2,
                            //    Lv3 = 3,
                            //    Lv4 = 4,
                            //    Sum = 10,
                            //    Remark = ""
                            //}

                        }
                    },
                     new DefectKindTableData(){
                        DefectType = "功能性",
                        Value = new List<DefectKindBaseValue>()
                        {
                            new DefectKindBaseValue(){
                                DefectName = "树根",
                                Lv1 = 1,
                                Lv2 = 2,
                                Lv3 = 3,
                                Lv4 = 4,
                                Sum = 10,
                                Remark = ""
                            },
                            new DefectKindBaseValue(){
                                DefectName = "沉积",
                                Lv1 = 1,
                                Lv2 = 2,
                                Lv3 = 3,
                                Lv4 = 4,
                                Sum = 10,
                                Remark = ""
                            },
                            new DefectKindBaseValue(){
                                DefectName = "障碍物",
                                Lv1 = 1,
                                Lv2 = 2,
                                Lv3 = 3,
                                Lv4 = 4,
                                Sum = 10,
                                Remark = ""
                            },
                            new DefectKindBaseValue(){
                                DefectName = "结垢",
                                Lv1 = 1,
                                Lv2 = 2,
                                Lv3 = 3,
                                Lv4 = 4,
                                Sum = 10,
                                Remark = ""
                            }

                        }
                    }
                }
            };
            InitData();
        }

        private void Model_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.model.grid.Height = this.model.panel.ActualWidth / 1.17;
        }

        private void OnOpenBetch()
        {
            //打开弹窗
            this.model.BetchPopup.IsOpen = true;

            //控制下箭头旋转成上箭头
            this.model.imageBottom.RenderTransform = new RotateTransform(180, this.model.imageBottom.Width / 2, this.model.imageBottom.Height / 2);
        }

        private string InitProName(string proName) {
            if (!string.IsNullOrWhiteSpace(proName)) {
                if (proName.Length > 8) {
                    proName = proName.Substring(0, 8) + "...";
                }
            }
            return proName;
        }

        /// <summary>
        /// 列表数据初始化
        /// </summary>
        public void InitData()
        {
            var titleList = new List<string>() { "缺陷类型", "缺陷名称", "1级", "2级", "3级", "4级", "合计", "备注" };

            //var rowCount = Data.DefectKind.SelectMany(p => p.Value).Count()+Data.DefectKind.Count+3;
            var rowCount = Data.DefectKind.SelectMany(p => p.Value).Count() + 1;
            for (int i = 0; i < rowCount; i++)
            {
                this.model.table.RowDefinitions.Add(new RowDefinition());

            }
            for (int i = 0; i < titleList.Count; i++)
            {
                if (i == 0)
                    this.model.table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(90) });
                else
                    this.model.table.ColumnDefinitions.Add(new ColumnDefinition());
            }

            //画标题
            for (int i = 0; i < titleList.Count; i++)
            {
                DrawCell(this.model.table, 0, i, titleList[i], new SolidColorBrush(Color.FromRgb(247, 248, 251)));
            }
            
            var firstRow = 1;
            var currentRow = 1;
            for (int i = 0; i < Data.DefectKind.Count; i++)
            {
                var item = Data.DefectKind[i];
                for (int j = 0; j < item.Value.Count; j++)
                {
                    var row = item.Value[j];
                    DrawRowSpanCell(this.model.table, currentRow, 0, item.DefectType,item.Value.Count, firstRow);
                    DrawCell(this.model.table, currentRow, 1, row.DefectName);
                    DrawCell(this.model.table, currentRow, 2, row.Lv1.ToString(), null, new SolidColorBrush(Color.FromRgb(38, 217, 115)));
                    DrawCell(this.model.table, currentRow, 3, row.Lv2.ToString(), null, new SolidColorBrush(Color.FromRgb(110, 163, 255)));
                    DrawCell(this.model.table, currentRow, 4, row.Lv3.ToString(),null, new SolidColorBrush(Color.FromRgb(255, 145, 71)));
                    DrawCell(this.model.table, currentRow, 5, row.Lv4.ToString(),null, new SolidColorBrush(Color.FromRgb(255, 87, 87)));
                    DrawCell(this.model.table, currentRow, 6, row.Sum.ToString());
                    DrawCell(this.model.table, currentRow, 7, row.Remark);

                    currentRow++;
                }

                //小计
                //{
                //    DrawRowSpanCell(this.model.table, currentRow, 0, item.DefectType, item.Value.Count+1, firstRow);
                //    DrawCell(this.model.table, currentRow, 1, "小计");
                //    DrawCell(this.model.table, currentRow, 2, item.Value.Sum(p=>p.Lv1).ToString(), null, new SolidColorBrush(Color.FromRgb(38, 217, 115)));
                //    DrawCell(this.model.table, currentRow, 3, item.Value.Sum(p => p.Lv2).ToString(), null, new SolidColorBrush(Color.FromRgb(110, 163, 255)));
                //    DrawCell(this.model.table, currentRow, 4, item.Value.Sum(p => p.Lv3).ToString(), null, new SolidColorBrush(Color.FromRgb(255, 145, 71)));
                //    DrawCell(this.model.table, currentRow, 5, item.Value.Sum(p => p.Lv4).ToString(), null, new SolidColorBrush(Color.FromRgb(255, 87, 87)));
                //    DrawCell(this.model.table, currentRow, 6, item.Value.Sum(p => p.Lv1 + p.Lv2+p.Lv3+p.Lv4).ToString());
                //    DrawCell(this.model.table, currentRow, 7, "");

                //    currentRow++;
                //}
                firstRow = currentRow ;
            }

            //合计
            //{
            //    DrawCell(this.model.table, currentRow, 0, "合计");
            //    DrawCell(this.model.table, currentRow, 1, "-");
            //    DrawCell(this.model.table, currentRow, 2, Data.DefectKind.SelectMany(p=>p.Value).Sum(p => p.Lv1).ToString());
            //    DrawCell(this.model.table, currentRow, 3, Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv2).ToString());
            //    DrawCell(this.model.table, currentRow, 4, Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv3).ToString());
            //    DrawCell(this.model.table, currentRow, 5, Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv4).ToString());
            //    DrawCell(this.model.table, currentRow, 6, Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv1 + p.Lv2 + p.Lv3 + p.Lv4).ToString());
            //    DrawCell(this.model.table, currentRow, 7, "");

            //    currentRow++;
            //}

            //缺陷等级占比
            //{
            //    var sum = Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv1 + p.Lv2 + p.Lv3 + p.Lv4);
            //    DrawCell(this.model.table, currentRow, 0, "缺陷等级占比");
            //    DrawCell(this.model.table, currentRow, 1, "-");
            //    DrawCell(this.model.table, currentRow, 2, (Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv1) * 100.0m / sum).ToString("N") + "%");
            //    DrawCell(this.model.table, currentRow, 3, (Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv2) * 100.0m / sum).ToString("N") + "%");
            //    DrawCell(this.model.table, currentRow, 4, (Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv3) * 100.0m / sum).ToString("N") + "%");
            //    DrawCell(this.model.table, currentRow, 5, (Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv4) * 100.0m / sum).ToString("N") + "%");
            //    DrawCell(this.model.table, currentRow, 6, (Data.DefectKind.SelectMany(p => p.Value).Sum(p => p.Lv1 + p.Lv2 + p.Lv3 + p.Lv4) * 100.0m / sum).ToString("N") + "%");
            //    DrawCell(this.model.table, currentRow, 7, "");
            //}
            //补边框
            for (int i = 0; i < rowCount; i++)
            {
                var border = new Border();
                border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));
                border.BorderThickness = new Thickness(0, 0, 1, 0);
                Grid.SetRow(border, i);
                Grid.SetColumn(border, titleList.Count - 1);
                this.model.table.Children.Add(border);
            }
            for (int i = 0; i < titleList.Count; i++)
            {
                var border = new Border();
                border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));
                border.BorderThickness = new Thickness(0, 0, 0, 1);
                Grid.SetRow(border, rowCount - 1);
                Grid.SetColumn(border, i);
                this.model.table.Children.Add(border);
            }
        }

        /// <summary>
        /// 画单元格
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="content"></param>
        public void DrawCell(Grid table, int row, int column, string content,
            SolidColorBrush background = null,
            SolidColorBrush foreground = null)
        {
            var label = new Label();
            label.HorizontalContentAlignment = HorizontalAlignment.Center;
            label.VerticalContentAlignment = VerticalAlignment.Center;
            label.FontSize = 13;
            label.Content = content;

            if (background != null)
                label.Background = background;
            if (foreground != null)
                label.Foreground = foreground;

            var border = new Border();
            border.BorderThickness = new Thickness(1, 1, 0, 0);
            border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));

            Grid.SetRow(border, row);
            Grid.SetColumn(border, column);

            Grid.SetRow(label, row);
            Grid.SetColumn(label, column);

            table.Children.Add(label);
            table.Children.Add(border);
        }
        /// <summary>
        /// 画合并单元格（rowspan）
        /// </summary>
        /// <param name="table"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <param name="content"></param>
        /// <param name="rowSpan"></param>
        /// <param name="firstRow"></param>
        public void DrawRowSpanCell(Grid table, int row, int column, string content,int rowSpan,int firstRow)
        {
            var label = new Label();
            label.HorizontalContentAlignment = HorizontalAlignment.Center;
            label.VerticalContentAlignment = VerticalAlignment.Center;
            label.FontSize = 13;
            label.Content = content;

            var border = new Border();
            border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));

            Grid.SetRow(border, row);
            Grid.SetColumn(border, column);

            Grid.SetRow(label, row);
            Grid.SetColumn(label, column);

            if (row == firstRow)
            {
                border.BorderThickness = new Thickness(1, 1, 0, 0);
                Grid.SetRowSpan(label, rowSpan);
                table.Children.Add(label);
                table.Children.Add(border);
            }
            else
            {
                border.BorderThickness = new Thickness(1, 0, 0, 0);
                table.Children.Add(border);
            }
            

           

            
            
        }

        public void InitData2()
        {
            var titleList = new List<string>() { "缺陷类型", "缺陷名称", "1级", "2级", "3级", "4级", "合计", "备注" };
            var columnList = new List<string>() { "结构性", "功能性", "合计", "缺陷等级占比" };
            var subColumnList = new List<string>() { "缺陷名称", "破裂", "渗漏", "异物穿入", "支管暗接", "起伏", "变形", "脱节", "脱落", "错口", "腐蚀", "小计", "树根", "沉积", "障碍物", "结垢", "小计", "", "" };

            for (int i = 0; i < subColumnList.Count; i++)
            {
                this.model.table.RowDefinitions.Add(new RowDefinition());

            }
            for (int i = 0; i < titleList.Count; i++)
            {
                if (i == 0)
                    this.model.table.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(90) });
                else
                    this.model.table.ColumnDefinitions.Add(new ColumnDefinition());
            }


            for (int i = 0; i < subColumnList.Count; i++)
            {
                for (int j = 0; j < titleList.Count; j++)
                {
                    var label = new Label();
                    label.HorizontalContentAlignment = HorizontalAlignment.Center;
                    label.VerticalContentAlignment = VerticalAlignment.Center;
                    label.FontSize = 13;

                    var border = new Border();
                    border.BorderThickness = new Thickness(1, 1, 0, 0);
                    if (j == titleList.Count - 1 && i == subColumnList.Count - 1)
                    {
                        border.BorderThickness = new Thickness(1, 1, 1, 1);
                    }
                    else if (j == titleList.Count - 1)
                    {
                        border.BorderThickness = new Thickness(1, 1, 1, 0);
                    }
                    else if (i == 18)
                    {
                        border.BorderThickness = new Thickness(1, 1, 0, 1);
                    }
                    border.BorderBrush = new SolidColorBrush(Color.FromRgb(230, 232, 243));
                    if (i == 0)
                    {
                        label.Background = new SolidColorBrush(Color.FromRgb(247, 248, 251));
                        label.Content = titleList[j];
                    }
                    else
                    {
                        label.Content = j;
                        switch (j)
                        {
                            case 2:
                                label.Foreground = new SolidColorBrush(Color.FromRgb(38, 217, 115));
                                break;
                            case 3:
                                label.Foreground = new SolidColorBrush(Color.FromRgb(110, 163, 255));
                                break;
                            case 4:
                                label.Foreground = new SolidColorBrush(Color.FromRgb(255, 145, 71));
                                break;
                            case 5:
                                label.Foreground = new SolidColorBrush(Color.FromRgb(255, 87, 87));
                                break;
                        }


                    }


                    Grid.SetRow(label, i);
                    Grid.SetColumn(label, j);

                    //第一列
                    if (j == 0)
                    {
                        switch (i)
                        {
                            case 0:
                                this.model.table.Children.Add(label);
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);

                                this.model.table.Children.Add(border);
                                break;
                            case 17:
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);
                                label.Content = columnList[2];
                                this.model.table.Children.Add(label);

                                this.model.table.Children.Add(border);
                                break;
                            case 18:
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);
                                label.Content = columnList[3];
                                this.model.table.Children.Add(label);

                                this.model.table.Children.Add(border);
                                break;
                            case 1:
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);
                                label.Content = columnList[0];
                                Grid.SetRowSpan(label, 11);
                                this.model.table.Children.Add(label);

                                this.model.table.Children.Add(border);
                                break;
                            case 12:
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);
                                label.Content = columnList[1];
                                Grid.SetRowSpan(label, 5);

                                this.model.table.Children.Add(label);

                                this.model.table.Children.Add(border);
                                break;
                            default:
                                border.BorderThickness = new Thickness(1, 0, 0, 0);
                                Grid.SetRow(border, i);
                                Grid.SetColumn(border, j);

                                this.model.table.Children.Add(border);
                                break;
                        }

                    }
                    else if (j == 1 && i > 0)//第二列
                    {
                        label.Content = subColumnList[i];
                        Grid.SetRow(border, i);
                        Grid.SetColumn(border, j);

                        this.model.table.Children.Add(label);
                        this.model.table.Children.Add(border);
                    }
                    else
                    {
                        Grid.SetRow(border, i);
                        Grid.SetColumn(border, j);

                        this.model.table.Children.Add(label);
                        this.model.table.Children.Add(border);
                    }

                }
            }

        }
        #region 图

        /// <summary>
        /// 柱状图集合
        /// </summary>
        public SeriesCollection ColunmSeriesCollection { get => UpdateSeries(); }

        /// <summary>
        /// 柱状图X坐标
        /// </summary>
        public List<string> ColumnXLabels { get; set; }
        public SeriesCollection SeriesCollection1 { get=> UpdatePieSeries(1,Data.DefectLv1); }
        public SeriesCollection SeriesCollection2 { get => UpdatePieSeries(2, Data.DefectLv2); }
        public SeriesCollection SeriesCollection3 { get => UpdatePieSeries(3, Data.DefectLv3); }
        public SeriesCollection SeriesCollection4 { get => UpdatePieSeries(4, Data.DefectLv4); }
        #endregion

        #region 方法

        /// <summary>
        /// 更新柱状图
        /// </summary>
        public SeriesCollection UpdateSeries()
        {
            var columnSeriesCollection = new SeriesCollection();

            ColumnXLabels = Data.DefectDense.Select(p => p.Label).ToList();

            List<double> columnValues = Data.DefectDense.Select(p => p.Value).ToList();
            
            ColumnSeries colunmseries = new ColumnSeries();
            colunmseries.DataLabels = true;
            colunmseries.ColumnPadding = 0;
            colunmseries.Title = "";
            colunmseries.Values = new ChartValues<double>(columnValues);
            columnSeriesCollection.Add(colunmseries);

            return columnSeriesCollection;
        }

        List<Color> colors = new List<Color>() {
            Color.FromRgb(38,217,115),
            Color.FromRgb(110,163,255),
            Color.FromRgb(255,145,71),
            Color.FromRgb(255,87,87)
        };

        /// <summary>
        /// 进度图数据初始化
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public SeriesCollection UpdatePieSeries(int level,int value)
        {
            var SeriesCollection = new SeriesCollection
            {
                new PieSeries
                {
                    Title = level+"级",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(value) },
                    DataLabels = true,
                    Fill = new SolidColorBrush(colors[level-1])
                },
                new PieSeries
                {
                    Title = "其他",
                    Values = new ChartValues<ObservableValue> { new ObservableValue(100-value)},
                    DataLabels = true,
                    Fill = new SolidColorBrush(Color.FromRgb(237,237,237))
                }
            };
            return SeriesCollection;
        }
        #endregion

        #region Command 
        public RelayCommand OpenBetch { get; private set; }
        #endregion
    }
}
